package com.ttbj.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * <p>Title: miduo360</p>
 * <p>Description: DateUtil </p>
 * <p>Copyright: Copyright (c) 2013</p>
 * <p>Company: Shanghai Mydream Information Technology Co.,Ltd</p>
 * @author
 * @version 1.0
 * history:
 * Date:          Resp            Comment
 * 2011-01-14     Selle Wu        Modify
 */

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class DateUtil {
    //private static Logger logger = (Logger) LogFactory.getLog( DateUtil.class );
	

	public static final long TIME_MONTH = 1000 * 60 * 60 * 24 * 30l;// 月
	/**
	 * 天数
	 */
	public static final long TIME_DAY = 1000 * 60 * 60 * 24l; // 天数
	/**
	 * 小时数
	 */
	public static final long TIME_HOUR = 1000 * 60 * 60;// 小时数
	/**
	 * 分钟数
	 */
	public static final long TIME_MINUTE = 1000 * 60;// 分钟数
	/**
	 * 秒数
	 */
	public static final long TIME_SECOND = 1000;// 秒
	
	

    public DateUtil() {
    }

    /**
     * 获取系统当前时间
     *
     * @return
     * @throws UtilDateException
     */
    public static java.sql.Date getCurrentSqlDate() {
        try {
            java.util.Date utilNow = new java.util.Date();
            java.sql.Date sqlNow = new java.sql.Date( utilNow.getTime() );
            return sqlNow;
        } catch ( Exception e ) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取系统当前时间
     *
     * @return
     * @throws UtilDateException
     */
    public static java.util.Date parseDate( java.sql.Date sqlDate ) {
        try {
            java.util.Date utilNow = new java.util.Date( sqlDate.getTime() );
            return utilNow;
        } catch ( Exception e ) {
            e.printStackTrace();
            return null;
        }
    }

    public static java.util.Date getCurrentDate() {
        try {
            java.util.Date utilNow = new java.util.Date();
            return utilNow;
        } catch ( Exception e ) {
            e.printStackTrace();
            return null;
        }
    }
    
    

    /**
     * 获取系统当前时间
     *
     * @return
     * @throws UtilDateException
     */
    public static java.sql.Timestamp getCurrentSqlTimestamp() {
        try {
            java.sql.Timestamp timestamp = new java.sql.Timestamp( System.currentTimeMillis() );
            return timestamp;
        } catch ( Exception e ) {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * 获取当前时间的字符串
     */
    public static String getCurrentDateStr(){
		return dateFormat(new Date(System.currentTimeMillis()),"yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 获取系统当前时间，格式为1970110时开始的毫秒
     *
     * @return
     * @throws UtilDateException
     */
    public static long getCurrentMillisecond() {
        return System.currentTimeMillis();
    }

    /**
     * 时间格式转换，将毫秒数转换为字符串型时间
     *
     * @param date
     *            毫秒
     * @param format
     *            格式
     * @return 字符串型时间
     */
    public static String dateFormat( long date, String format ) {
        try {
            // date = subTimeZoneOffset(date);
            if ( ( format == null ) || ("".equals(format)) ) {
                return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(date));
            } else {
                return new SimpleDateFormat(format).format(new Date(date));
            }
        } catch ( Exception e ) {
            return "";
        }
    }

    /**
     * 时间格式转换，将字符串型时间转换为毫秒数 "yyyy-MM-dd HH:mm:ss" => "12345" 19 "yyyyMMddHHmmss"
     * => "12345" 14 "yyyy-MM-dd" => "12345" 10 返回 0 是格式不
     *
     * @param dateStr
     * @return
     */
    public static long dateFormat( String dateStr ) {
        dateStr = dateStr.trim();
        if ( dateStr.length() == 19 ) {
            try {
                java.util.Calendar cal = java.util.Calendar.getInstance();
                cal.set( Integer.parseInt( dateStr.substring( 0, 4 ) ), Integer.parseInt( dateStr.substring( 5, 7 ) ) - 1, Integer.parseInt( dateStr.substring( 8, 10 ) ), Integer.parseInt( dateStr.substring( 11, 13 ) ),
                        Integer.parseInt( dateStr.substring( 14, 16 ) ), Integer.parseInt( dateStr.substring( 17, 19 ) ) );
                cal.set( java.util.Calendar.MILLISECOND, 0 );
                return ( cal.getTime().getTime() );
            } catch ( Exception e ) {
                return 0;
            }

        } else if ( dateStr.length() == 14 ) {
            try {
                java.util.Calendar cal = java.util.Calendar.getInstance();
                cal.set( Integer.parseInt( dateStr.substring( 0, 4 ) ), Integer.parseInt( dateStr.substring( 4, 6 ) ) - 1, Integer.parseInt( dateStr.substring( 6, 8 ) ), Integer.parseInt( dateStr.substring( 8, 10 ) ),
                        Integer.parseInt( dateStr.substring( 10, 12 ) ), Integer.parseInt( dateStr.substring( 12, 14 ) ) );
                cal.set( java.util.Calendar.MILLISECOND, 0 );
                return ( cal.getTime().getTime() );
            } catch ( Exception e ) {
                return 0;
            }
        } else if ( dateStr.length() == 10 ) {
            try {
                java.util.Calendar cal = java.util.Calendar.getInstance();
                cal.set( Integer.parseInt( dateStr.substring( 0, 4 ) ), Integer.parseInt( dateStr.substring( 5, 7 ) ) - 1, Integer.parseInt( dateStr.substring( 8, 10 ) ), 0, 0, 0 );
                cal.set( java.util.Calendar.MILLISECOND, 0 );
                return ( cal.getTime().getTime() );
            } catch ( Exception e ) {
                return 0;
            }
        } else {
            return 0;
        }

    }

    /**
     * 时间格式转换，将字符串型时间转换为毫秒数 "yyyy-MM-dd HH:mm:ss" => "12345" 19 "yyyyMMddHHmmss"
     * => "12345" 14 "yyyy-MM-dd" => "12345" 10 返回 0 是格式不
     *
     * @param dateStr
     * @return
     */
    public static long parseStringToLong( String dateStr ) {
        dateStr = dateStr.trim();
        if ( dateStr.length() == 19 ) {
            try {
                java.util.Calendar cal = java.util.Calendar.getInstance();
                cal.set( Integer.parseInt( dateStr.substring( 0, 4 ) ), Integer.parseInt( dateStr.substring( 5, 7 ) ) - 1, Integer.parseInt( dateStr.substring( 8, 10 ) ), Integer.parseInt( dateStr.substring( 11, 13 ) ),
                        Integer.parseInt( dateStr.substring( 14, 16 ) ), Integer.parseInt( dateStr.substring( 17, 19 ) ) );
                cal.set( java.util.Calendar.MILLISECOND, 0 );
                return ( cal.getTime().getTime() );
            } catch ( Exception e ) {
                return 0;
            }

        } else if ( dateStr.length() == 14 ) {
            try {
                java.util.Calendar cal = java.util.Calendar.getInstance();
                cal.set( Integer.parseInt( dateStr.substring( 0, 4 ) ), Integer.parseInt( dateStr.substring( 4, 6 ) ) - 1, Integer.parseInt( dateStr.substring( 6, 8 ) ), Integer.parseInt( dateStr.substring( 8, 10 ) ),
                        Integer.parseInt( dateStr.substring( 10, 12 ) ), Integer.parseInt( dateStr.substring( 12, 14 ) ) );
                cal.set( java.util.Calendar.MILLISECOND, 0 );
                return ( cal.getTime().getTime() );
            } catch ( Exception e ) {
                return 0;
            }
        } else if ( dateStr.length() == 10 ) {
            try {
                java.util.Calendar cal = java.util.Calendar.getInstance();
                cal.set( Integer.parseInt( dateStr.substring( 0, 4 ) ), Integer.parseInt( dateStr.substring( 5, 7 ) ) - 1, Integer.parseInt( dateStr.substring( 8, 10 ) ), 0, 0, 0 );
                cal.set( java.util.Calendar.MILLISECOND, 0 );
                return ( cal.getTime().getTime() );
            } catch ( Exception e ) {
                return 0;
            }
        } else {
            try {
                return Long.parseLong( dateStr );
            } catch ( Exception e ) {
                return 0;
            }

        }

    }

    /**
     * 转换时间 long -> String
     *
     * @param timestamp
     *            传入系统时间
     * @return String
     */
    public static String parseLongToString( long timestamp ) {
        SimpleDateFormat bartDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        String aa = null;
        try {
            java.util.Date date = new java.util.Date( timestamp );
            aa = bartDateFormat.format( date );
        } catch ( Exception ex ) {
            //logger.error( ex.getMessage() );
        }
        return aa;
    }

    /**
     * 转换时间 long -> String 时间格式：yyyy-MM-dd
     *
     * @param timestamp
     *            传入系统时间
     * @return String
     */
    public static String parseLongToString1( long timestamp ) {
        SimpleDateFormat bartDateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
        String aa = null;
        try {
            java.util.Date date = new java.util.Date( timestamp );
            aa = bartDateFormat.format( date );
        } catch ( Exception ex ) {
            //logger.error( ex.getMessage() );
        }
        return aa;
    }

    /**
     * 根据日期字符串、日期和时间的格式获取日期
     *
     * @date 2016年11月24日 下午1:50:12
     *
     * @param dateString 日期字符串
     * @param dateFormat 日期和时间的格式
     * @param defaultValue 默认日期
     * @return
     */
    public static java.util.Date getDate( String dateString, String dateFormat, java.util.Date defaultValue ) {
        if ( dateString == null ) {
            return defaultValue;
        }

        SimpleDateFormat formatter = new SimpleDateFormat( dateFormat );

        try {
            if ( dateString.length() < 11 ) {
                dateString += " 00:00:00";
            }
            if ( dateString.indexOf( "/" ) > -1 ) {
                dateString = StringUtils.replace( dateString, "/", "-" );
            }
            return formatter.parse( dateString );
        } catch ( ParseException e ) {

        }

        return defaultValue;
    }

    public static java.util.Date getDateDf( String name, String df ) {
        return getDate( name, df, null );
    }

    public static java.util.Date getDate( String name, String df ) {
        return getDate( name, df, null );
    }

    /**
     * 根据日期字符串获取日期：yyyy-MM-dd
     *
     * @date 2016年11月24日 下午1:55:51
     *
     * @param name
     * @return
     */
    public static java.util.Date getDate( String name ) {
        return getDate( name, "yyyy-MM-dd", null );
    }

    /**
     * 根据日期字符串获取日期：yyyy-MM
     *
     * @date 2016年11月24日 下午1:56:10
     *
     * @param name
     * @return
     */
    public static java.util.Date getMonth( String name ) {
        return getDate( name, "yyyy-MM", null );
    }

    public static java.util.Date getDateTime( String name ) {
        return getDateTime( name, null );
    }

    /**
     * 根据日期字符串获取日期：yyyy-MM-dd
     *
     * @date 2016年11月24日 下午1:54:20
     *
     * @param name
     * @param defaultValue
     * @return
     */
    public static java.util.Date getDate( String name, java.util.Date defaultValue ) {
        return getDate( name, "yyyy-MM-dd", defaultValue );
    }

    /**
     * 根据日期字符串获取日期：yyyy-MM-dd HH:mm:ss
     *
     * @date 2016年11月24日 下午1:55:00
     *
     * @param name
     * @param defaultValue
     * @return
     */
    public static java.util.Date getDateTime( String name, java.util.Date defaultValue ) {
        return getDate( name, "yyyy-MM-dd HH:mm:ss", defaultValue );
    }

    /**
     * 时间格式转换，将毫秒数转换为字符串型时间
     *
     * @param date
     *            java.util.Date
     * @param format
     *            格式
     * @return 字符串型时间
     */
    public static String dateFormat( java.util.Date date, String format ) {
        try {
            // date = subTimeZoneOffset(date);
            if ( ( format == null ) || ("".equals(format)) ) {
                return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
            } else {
                return new SimpleDateFormat(format).format(date);
            }
        } catch ( Exception e ) {
            return "";
        }
    }

    public static java.sql.Timestamp parseTimestamp( java.util.Date utilDate ) {
        try {
            Timestamp timestamp = new Timestamp( utilDate.getTime() );

            return timestamp;
        } catch ( Exception e ) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     *
     * @param oldTime
     * @return
     */
    public static boolean compareDates(String oldTime) {
        if(System.currentTimeMillis() > dateFormat(oldTime)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     *
     * @param oldTime
     * @return
     */
    public static boolean compareDates(String time1,String time2) {
        if(dateFormat(time1) >= dateFormat(time2)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断当前时间是否比oldTime晚second秒,如果是返回true,否则返回false
     * @param oldTime
     * @param second
     * @return boolean
     */
    public static boolean timeToJudge(String oldTime,int second) {
        //把秒设置成毫秒
        long millisecond = second * 1000;
        //当前时间和原始时间的差毫秒
        long poorMs = System.currentTimeMillis() - dateFormat(oldTime);
        //如果当前时间和原始时间查毫秒比millisecond大，返回true
        if(poorMs >= millisecond) {
            return true;
        } else {
            return false;
        }
    }

    /**
     *
     * @param second
     * @return
     */
    public static Map<String,String> getStartAndEnd(int second) {
        //把秒设置成毫秒
        long millisecond = second * 1000;
        long start = System.currentTimeMillis();
        long end = start + millisecond;
        Map<String, String> map = new HashMap<String, String>();
        map.put("start", dateFormat(start,"yyyyMMddHHmmss"));
        map.put("end", dateFormat(end,"yyyyMMddHHmmss"));
        return map;
    }

    public static String getCurDtStr() {
        Date nowTime = new Date(System.currentTimeMillis());
        SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyyMMdd");
        return sdFormatter.format(nowTime);
    }

    /**
     * 根据datePtn获取当前时间字符串：
     *
     * @date 2016年11月24日 上午11:57:58
     *
     * @param datePtn  如：yyyyMMddHHmmss
     * @return
     */
    public static String getCurDtStr(String datePtn) {
        Date nowTime = new Date(System.currentTimeMillis());
        SimpleDateFormat sdFormatter = new SimpleDateFormat(datePtn);
        return sdFormatter.format(nowTime);
    }

    /**
     * 获取当前时间戳字符串：yyyyMMddHHmmss
     *
     * @date 2016年11月24日 上午11:56:37
     *
     * @return
     */
    public static String getCurDtsStr() {
        Date nowTime = new Date(System.currentTimeMillis());
        SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdFormatter.format(nowTime);
    }

    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

    public static String getWeekOfDate2(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

    public static String getCurDtWeekStr() {
        Date nowTime = new Date(System.currentTimeMillis());
        return getWeekOfDate2(nowTime);
    }

    /**
     * 根据日期字符串（yyyyMMddHHmmss***），转换成日期字符串（yyyy-MM-dd）
     * <br/>
     * 日期字符串长度必须大于8
     *
     * @date 2016年11月24日 下午1:57:47
     *
     * @param date
     * @return
     */
    public static String stringDateFormat(String date){
        if (date.isEmpty() || date.length()<=8) {
            return date;
        }
        String reString = date.substring(0, 4)+"-"+date.substring(4, 6)+"-"+date.substring(6, 8);
        return reString;
    }

    /**
     * 获得有中文的当天日期(2014年5月22日)
     * @return
     */
    public static String getChineseFormatDate() {
        String today = getCurDtStr();
        String chieseDate = today.substring(0, 4);
        if("0".equals(today.substring(4, 5))) {
            chieseDate+="年"+today.substring(5, 6);
        } else {
            chieseDate+="年"+today.substring(4, 6);
        }
        if("0".equals(today.substring(6, 7))) {
            chieseDate+="月"+today.substring(7, 8);
        } else {
            chieseDate+="月"+today.substring(6, 8);
        }
        return chieseDate+"日";
    }

    /**
     * 获得有中文的当天日期(2014年5月22日)
     * @return
     */
    public static String getChineseFormatDate(String dts) {
        String chieseDate = dts.substring(0, 4);
        if("0".equals(dts.substring(4, 5))) {
            chieseDate+="年"+dts.substring(5, 6);
        } else {
            chieseDate+="年"+dts.substring(4, 6);
        }
        if("0".equals(dts.substring(6, 7))) {
            chieseDate+="月"+dts.substring(7, 8);
        } else {
            chieseDate+="月"+dts.substring(6, 8);
        }
        return chieseDate+"日";
    }

    public static String getDtStrByInterval(String format, int unit, int interval){
        Calendar cal = Calendar.getInstance();
        cal.add(unit, interval);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String rslt = sdf.format(cal.getTime());
        return rslt;
    }

    public static String getDtStrByInterval(String fromDt, String format, int unit, int interval) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(fromDt));
        cal.add(unit, interval);
        String rslt = sdf.format(cal.getTime());
        return rslt;
    }

    public static String getDtStrByIntervalForRedEnvelope(int unit, int interval){
        String format = "yyyyMMdd";
        Calendar cal = Calendar.getInstance();
        cal.add(unit, interval);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String rslt = sdf.format(cal.getTime()) + "235959";
        return rslt;
    }

    /**
     * 获得从startDt到endDt时间的差(0天18小时0分)
     * @param startDt
     * @param endDt
     * @return
     */
    public static String getTimeDifferenceDHM(String startDt,String endDt) {
        String diffStr = null;
        if(StringUtils.isNotBlank(startDt) || StringUtils.isNotBlank(endDt)) {
            SimpleDateFormat sd = new SimpleDateFormat("yyyyMMddHHmmss");
            long nd = 1000*24*60*60;//一天的毫秒数
            long nh = 1000*60*60;//一小时的毫秒数
            long nm = 1000*60;//一分钟的毫秒数
            long ns = 1000;//一秒钟的毫秒数
            long diff;
            try {
                diff = sd.parse(endDt).getTime() - sd.parse(startDt).getTime();
                long day = diff/nd;//计算差多少天
                long hour = diff%nd/nh;//计算差多少小时
                long min = diff%nd%nh/nm;//计算差多少分钟
                long sec = diff%nd%nh%nm/ns;//计算差多少秒
                diffStr = day+"天"+hour+"时"+min+"分"+sec+"秒";
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            return null;
        }
        return diffStr;
    }


    /**
     * 获得从startDt到endDt时间的差(0天18小时0分)
     * @param startDt
     * @param endDt
     * @return
     */
    public static String getTimeDifferenceDHMStr(String startDt,String endDt) {
        String timestr = null;
        if(StringUtils.isNotBlank(startDt) || StringUtils.isNotBlank(endDt)) {
            SimpleDateFormat sd = new SimpleDateFormat("yyyyMMddHHmmss");
            long nd = 1000*24*60*60;//一天的毫秒数
            long nh = 1000*60*60;//一小时的毫秒数
            long nm = 1000*60;//一分钟的毫秒数
            long ns = 1000;//一秒钟的毫秒数
            long diff;
            try {
                diff = sd.parse(endDt).getTime() - sd.parse(startDt).getTime();
                long day = diff/nd;//计算差多少天
                long hour = diff%nd/nh;//计算差多少小时
                long min = diff%nd%nh/nm;//计算差多少分钟
                long sec = diff%nd%nh%nm/ns;//计算差多少秒
                timestr = day+","+hour+","+min+","+sec;
                return timestr;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            return null;
        }
        return timestr;
    }

    /**
     *
     * @param second
     * @return
     */
    public static String secondToStr(long second) {
        StringBuffer sb = new StringBuffer();
        if(second>0) {
            long nd = 1000*24*60*60;//一天的毫秒数
            long nh = 1000*60*60;//一小时的毫秒数
            long nm = 1000*60;//一分钟的毫秒数
            long ns = 1000;//一秒钟的毫秒数
            long diff;
            diff = second * 1000;
            long day = diff/nd;//计算差多少天
            long hour = diff%nd/nh;//计算差多少小时
            long min = diff%nd%nh/nm;//计算差多少分钟
            long sec = diff%nd%nh%nm/ns;//计算差多少秒
            if(day>0) {
                sb.append(day+"天");
            }
            if(hour>0) {
                sb.append(hour+"时");
            }
            if(min>0) {
                sb.append(min+"分");
            }
            if(sec>0) {
                sb.append(sec+"秒");
            }
        } else {
            return null;
        }
        return sb.toString();
    }

    /**
     *
     * @param second
     * @return
     */
    public static String secondToStr(int second) {
        StringBuffer sb = new StringBuffer();
        if(second>0) {
            long nd = 1000*24*60*60;//一天的毫秒数
            long nh = 1000*60*60;//一小时的毫秒数
            long nm = 1000*60;//一分钟的毫秒数
            long ns = 1000;//一秒钟的毫秒数
            long diff;
            diff = second * 1000;
            long day = diff/nd;//计算差多少天
            long hour = diff%nd/nh;//计算差多少小时
            long min = diff%nd%nh/nm;//计算差多少分钟
            long sec = diff%nd%nh%nm/ns;//计算差多少秒
            if(day>0) {
                sb.append(day+"天");
            }
            if(hour>0) {
                sb.append(hour+"时");
            }
            if(min>0) {
                sb.append(min+"分");
            }
            if(sec>0) {
                sb.append(sec+"秒");
            }
        } else {
            return null;
        }
        return sb.toString();
    }

    /**
     *
     * @param time
     * @return
     */
    public static String getGMTDateStr(long time) {
        Date d = new Date(System.currentTimeMillis() + time);
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
        return dateFormat.format(d);
    }

    public static String getGMTDateStr(long time, Locale locale) {
        Date d = new Date(System.currentTimeMillis() + time);
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", locale);
        return dateFormat.format(d);
    }

    /**
     * 计算两个日期之间相差的天数
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }


    /**
     *字符串的日期格式的计算
     */
    public static int daysBetween(String smdate, String bdate) throws ParseException {
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//		Calendar cal = Calendar.getInstance();
//		cal.setTime(sdf.parse(smdate));
//		long time1 = cal.getTimeInMillis();
//		cal.setTime(sdf.parse(bdate));
//		long time2 = cal.getTimeInMillis();
//		long between_days = (time2 - time1) / (1000 * 3600 * 24);
//
//		return Integer.parseInt(String.valueOf(between_days));
        return daysBetween(smdate, bdate, "yyyyMMdd");
    }

    /**
     *字符串的日期格式的计算
     */
    public static int daysBetween(String smdate, String bdate,String format) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    public static String monthDay(String date,Integer month) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(simpleDateFormat.parse(date));
        cal.add(Calendar.MONTH, month);
        return simpleDateFormat.format(cal.getTime());
    }

    public static String getMonthFirstDay(String formatStr,int month) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        return format.format(c.getTime());
    }

    public static String getMonthLastDay(String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        return format.format(ca.getTime());
    }

    public static String beforeOneHourToNowDate(int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hour);
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        return df.format(calendar.getTime());
    }

    public static String SubDay(String date, Integer days) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(simpleDateFormat.parse(date));
        cal.add(Calendar.DATE, days);
        return simpleDateFormat.format(cal.getTime());
    }

    public static String dateFormat(String df, String dts) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        try {
            date = sdf.parse(dts);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(df);

        return simpleDateFormat.format(date);
    }
    
    public static String dateFormat(String curdf,String oftdf,String dts){
    	  SimpleDateFormat sdf = new SimpleDateFormat(curdf);
          Date date = new Date();
          try {
              date = sdf.parse(dts);
          } catch (ParseException e) {
              e.printStackTrace();
          }

          SimpleDateFormat simpleDateFormat = new SimpleDateFormat(oftdf);

          return simpleDateFormat.format(date);
    	
    }

    /**
     * 获取当前时间各个单位值
     * @param Calendar.YEAR、Calendar.MONTH、Calendar.DATE、Calendar.HOUR_OF_DAY、Calendar.MINUTE、Calendar.SECOND
     * @return
     */
    public static int getCurVal(int calendarVal) {
        Calendar cal = Calendar.getInstance();
        return cal.get(calendarVal);
    }

    /**
     * 得到两日期相差几个月
     *
     * @param String
     * @return
     */
    public static long getMonth(String startDate, String endDate) {
        if (startDate.equals(endDate)) {
            return 1;
        }

        SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
        long monthday;
        try {
            Date startDate1 = f.parse(startDate);
            //开始时间与今天相比较
            Date endDate1 = f.parse(endDate);

            Calendar starCal = Calendar.getInstance();
            starCal.setTime(startDate1);

            int sYear = starCal.get(Calendar.YEAR);
            int sMonth = starCal.get(Calendar.MONTH);
            int sDay = starCal.get(Calendar.DATE);

            Calendar endCal = Calendar.getInstance();
            endCal.setTime(endDate1);
            int eYear = endCal.get(Calendar.YEAR);
            int eMonth = endCal.get(Calendar.MONTH);
            int eDay = endCal.get(Calendar.DATE);

            monthday = ((eYear - sYear) * 12 + (eMonth - sMonth));

            if (sDay < eDay) {
                monthday = monthday + 1;
            }
            return monthday;
        } catch (ParseException e) {
            monthday = 0;
        }
        return monthday;
    }

    public static String getMonthDts(int month) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, month);
        return dateFormat(c.getTime(), "yyyyMMdd");
    }

    /**
     * 获取后n天
     *
     * @param date
     * @return Date
     */
    public static Date getNextNDate(Date date, int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE, day + n);
        return calendar.getTime();
    }

    /**
     * 获取前n天
     *
     * @param date
     * @return Date
     */
    public static Date getPreviousNDate(Date date, int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE, day - n);
        return calendar.getTime();
    }

    public static String addDate(int period, String unit) {
        String now = getCurDtsStr();
        return addDate(now, period, unit);
    }

    public static String addDate(String start, int period, String unit) {
        SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            Date startDate1 = f.parse(start);

            Calendar starCal = Calendar.getInstance();
            starCal.setTime(startDate1);
            if ("D".equals(unit)) {
                starCal.add(Calendar.DATE, period);
            } else if ("M".equals(unit)) {
                starCal.add(Calendar.MONTH, period);
            } else {
                starCal.add(Calendar.YEAR, period);
            }
            return f.format(starCal.getTime());
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 追加天数
     * @param
     * @author Daniel
     * @description
     * @date 2017/12/7
     */
    public static String addDay(String strDate, int i) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        }
        Date date = getDate(strDate, "yyyyMMdd");
        Calendar c = Calendar.getInstance();
        c.setTime(date);   //设置当前日期
        c.add(Calendar.DATE, i); //日期加1天
        date = c.getTime();
        return dateFormat(date, "yyyyMMdd");
    }
}


